<?php
/**
 * 书籍分类
 * User: Richard
 * Date: 2020/3/14
 * Time: 16:05
 */

namespace Demo\Controller;


use Common\Controller\DemoBaseController;

class BookCategoryController extends DemoBaseController
{
    private $bookCategoryModel;

    function __construct()
    {
        parent::__construct();
        $this->bookCategoryModel = M('book_categorys');
    }


    /**
     * 书籍分类列表
     */
    public function index()
    {
        $pageCount = I('count', 20, 'intval');//一页20个

        $where = ['status' => 1];
        $name = I('name');
        if (!empty($name)) {
            $where['name|desc'] = ['like', '%' . $name . '%'];
        }

        $count = $this->bookCategoryModel->where($where)->count();
        $page = $this->page($count, $pageCount);

        $list = $this->bookCategoryModel->where($where)->limit($page->firstRow, $page->listRows)->select();
        $this->returnok(['list' => $list, 'page' => $page]);
    }

    /**
     * 添加书籍提交
     */
    public function add_post()
    {
        $name = I('name');
        if (empty($name)) {
            $this->returnerror('', '请填写书籍分类名称');
        }
        $bg = I('bg');
        if (empty($author)) {
            $this->returnerror('', '请上传分类图片');
        }
        $desc = I('desc');

        $data = [
            'name' => $name,//书籍分类名称
            'bg' => $bg,//分类图片
            'desc' => $desc,//分类描述
        ];
        $id = $this->bookCategoryModel->add($data);
        if ($id) {
            $this->returnok(['id' => $id], '添加成功');
        } else {
            $this->returnerror('', '添加失败，请重试');
        }
    }

    /**
     * 编辑书籍分类
     */
    public function edit()
    {
        $categoryId = I('id', 0, 'intval');
        if (empty($categoryId)) {
            $this->returnerror('', '无效请求');
        }
        $info = $this->bookCategoryModel->where(['id' => $categoryId, 'status' => 1])->find();
        if (empty($info)) {
            $this->returnerror('', '未找到书籍分类或书籍分类已被管理员删除');
        }
        $this->returnok($info);
    }

    /**
     * edit书籍提交
     */
    public function edit_post()
    {
        $categoryId = I('id', 0, 'intval');
        if (empty($categoryId)) {
            $this->returnerror('', '无效请求');
        }
        $name = I('name');
        if (empty($name)) {
            $this->returnerror('', '请填写书籍分类名称');
        }
        $bg = I('bg');
        if (empty($author)) {
            $this->returnerror('', '请上传分类图片');
        }
        $desc = I('desc');

        $data = [
            'name' => $name,//书籍分类名称
            'bg' => $bg,//分类图片
            'desc' => $desc,//分类描述
        ];

        $res = $this->bookCategoryModel->where(['id' => $categoryId])->save($data);
        if ($res !== false || $res === 0) {
            $this->returnok(['id' => $categoryId], '修改成功');
        } else {
            $this->returnerror('', '修改失败，请重试');
        }
    }

    /**
     * 书籍分类软删除
     */
    public function softDelete()
    {
        $categoryId = I('id', 0, 'intval');
        if (empty($categoryId)) {
            $this->returnerror('', '无效请求');
        }
        $res = $this->bookCategoryModel->where(['id' => $categoryId])->save(['status' => 0]);
        if ($res !== false || $res === 0) {
            $this->returnok(['id' => $categoryId], '删除成功');
        } else {
            $this->returnerror('', '删除失败，请重试');
        }
    }

    /**
     * 软删除恢复
     */
    public function reply()
    {
        $categoryId = I('id', 0, 'intval');
        if (empty($categoryId)) {
            $this->returnerror('', '无效请求');
        }
        $res = $this->bookCategoryModel->where(['id' => $categoryId])->save(['status' => 1]);
        if ($res !== false || $res === 0) {
            $this->returnok(['id' => $categoryId], '恢复成功');
        } else {
            $this->returnerror('', '恢复失败，请重试');
        }
    }
}